﻿using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContext
    {
        public static class ContextMenuRoot
        {
            public const string ContextDomain = "/Skinn: Edit";
            public const int ContextDomainPostion = 211;
            public const int GameObjectContextPosition = 12;

            [MenuItem("GameObject/Create Skinn/TexturePacker", false, GameObjectContextPosition)]
            public static void CreateTexturePacker(MenuCommand command)
            {
                var go = new GameObject("Texture Packer", new System.Type[1] { typeof(TexturePacker) });
                Undo.RegisterCreatedObjectUndo(go, "Create TexturePacker");
                Selection.activeObject = go;
            }

            [MenuItem("GameObject/Create Skinn/Vertex Mapper", false, GameObjectContextPosition)]
            private static void CreateVertexMapper(MenuCommand command)
            {
                var go = new GameObject("Vertex Mapper", new System.Type[1] { typeof(VertexMapper) });
                Undo.RegisterCreatedObjectUndo(go, "Create VM");
                Selection.activeObject = go;
            }

            [MenuItem("GameObject" + ContextDomain, false, GameObjectContextPosition)]
            private static void ShowContextMenuGameObject(MenuCommand command)
            {
                var go = (GameObject)command.context;

                var smr = go.GetComponent<SkinnedMeshRenderer>();
                var mr = go.GetComponent<MeshRenderer>();

                Component context = go.transform;

                if (smr)
                {
                    ContextType = ContextID.SkinnedMeshRenderer;
                    context = smr;
                }
                else if(mr)
                {
                    ContextType = ContextID.MeshRenderer;
                    context = mr;
                }
                else
                {
                    ContextType = ContextID.Transform;
                    context = go.transform;
                }

                var instanceID = context.gameObject.GetInstanceID();
                if (!InstanceIDs.Contains(instanceID)) InstanceIDs.Add(instanceID);
                MenuMain.ContextShown = false;
                EditorGUIUtility.PingObject(context);
            }

            [MenuItem("GameObject" + ContextDomain, true)]
            private static bool CanShowContextMenuGameObject(MenuCommand command)
            {
                var go = (GameObject)command.context;
                if (EditorApplication.isCompiling) return false;
                if (SkinnEx.IsNullOrNotInAScene(go)) return false;
                return true;
            }

            [MenuItem("CONTEXT/SkinnedMeshRenderer" + ContextDomain, false, ContextDomainPostion)]
            private static void ShowContextMenuSMR(MenuCommand command)
            {
                ContextType = ContextID.SkinnedMeshRenderer;
                var context = (Component)command.context;
                var instanceID = context.gameObject.GetInstanceID();
                if (!InstanceIDs.Contains(instanceID)) InstanceIDs.Add(instanceID);
                MenuMain.ContextShown = false;
                EditorGUIUtility.PingObject(context);
            }

            [MenuItem("CONTEXT/SkinnedMeshRenderer" + ContextDomain, true)]
            private static bool CanShowContextMenuSMR(MenuCommand command)
            {
                var context = (Renderer)command.context;
                if (EditorApplication.isCompiling) return false;
                if (SkinnEx.IsNullOrNotInAScene(context)) return false;
                return true;
            }

            [MenuItem("CONTEXT/MeshRenderer" + ContextDomain, false, ContextDomainPostion)]
            private static void ShowContextMenuMR(MenuCommand command)
            {
                ContextType = ContextID.MeshRenderer;
                var context = (Component)command.context;
                var instanceID = context.gameObject.GetInstanceID();
                if (!InstanceIDs.Contains(instanceID)) InstanceIDs.Add(instanceID);
                MenuMain.ContextShown = false;
                EditorGUIUtility.PingObject(context);
            }

            [MenuItem("CONTEXT/MeshRenderer" + ContextDomain, true)]
            private static bool CanShowContextMenuMR(MenuCommand command)
            {
                var context = (Renderer)command.context;
                if (EditorApplication.isCompiling) return false;
                if (SkinnEx.IsNullOrNotInAScene(context)) return false;
                return true;
            }

            [MenuItem("CONTEXT/MeshFilter" + ContextDomain, false, ContextDomainPostion)]
            private static void ShowContextMenuMF(MenuCommand command)
            {
                ContextType = ContextID.MeshRenderer;
                var context = (Component)command.context;
                var instanceID = context.gameObject.GetInstanceID();
                if (!InstanceIDs.Contains(instanceID)) InstanceIDs.Add(instanceID);
                MenuMain.ContextShown = false;
                EditorGUIUtility.PingObject(context);
            }

            [MenuItem("CONTEXT/MeshFilter" + ContextDomain, true)]
            private static bool CanShowContextMenuMF(MenuCommand command)
            {
                var context = ((Component)command.context).GetComponent<MeshRenderer>();
                if (EditorApplication.isCompiling) return false;
                if (SkinnEx.IsNullOrNotInAScene(context)) return false;
                return true;
            }

            [MenuItem("CONTEXT/Transform" + ContextDomain, false, ContextDomainPostion)]
            private static void ShowContextMenuTransform(MenuCommand command)
            {
                ContextType = ContextID.Transform;
                var context = (Component)command.context;
                var instanceID = context.gameObject.GetInstanceID();
                if (!InstanceIDs.Contains(instanceID)) InstanceIDs.Add(instanceID);
                MenuMain.ContextShown = false;
                EditorGUIUtility.PingObject(context);
            }

            [MenuItem("CONTEXT/Transform" + ContextDomain, true)]
            private static bool CanShowContextMenuTransform(MenuCommand command)
            {
                var context = (Transform)command.context;
                if (EditorApplication.isCompiling) return false;
                if (SkinnEx.IsNullOrNotInAScene(context)) return false;
                return true;
            }

        }
    }
}
